package blog.com.mw.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import blog.com.mw.base.ResultData;
import blog.com.mw.dao.LabelDao;
import blog.com.mw.pojo.po.LabelPO;
import blog.com.mw.pojo.po.LabelTypePO;
import blog.com.mw.pojo.vo.LabelTypeVO;
import blog.com.mw.pojo.vo.LabelVO;
import blog.com.mw.service.LabelService;

@Service
public class LabelServiceImpl implements LabelService {

	@Autowired
	private LabelDao dao;
	@Override
	public ResultData getLabelById(int id) {
		ResultData rs = null;
		Map<String,Object> data = null;
		try {
			LabelPO po = dao.getLabelById(Long.valueOf(id));
			if(po != null) {
				data = new HashMap<>();
				data.put("data", po);
				rs = ResultData.getOk();
				rs.setData(data);
				rs.setMsg("获取成功");
			}else {
				rs = ResultData.getFail();
				rs.setMsg("标签不存在！");
			}
		}catch(Exception e) {
			e.printStackTrace();
			rs = ResultData.getFail();
			rs.setMsg("未知异常");
		}
		return rs;
	}

	@Override
	public ResultData getLabelByContent(String content) {
		ResultData rs = null;
		Map<String,Object> data = null;
		try {
			if(StringUtils.isEmpty(content)) {
				rs = ResultData.getFail();
				rs.setMsg("参数不得为空！");
				return rs;
			}
			LabelPO po = dao.getLabelByContent(content);
			if(po != null) {
				data = new HashMap<>();
				data.put("data", po);
				rs = ResultData.getOk();
				rs.setData(data);
				rs.setMsg("获取成功");
			}else {
				rs = ResultData.getFail();
				rs.setMsg("获取失败");
			}
		}catch(Exception e) {
			e.printStackTrace();
			rs = ResultData.getFail();
			rs.setMsg("未知异常");
		}
		return rs;
	}

	@Override
	public ResultData getLabelByArticleId(int articleId) {
		ResultData rs = null;
		Map<String,Object> data = null;
		try {
			List<LabelPO> pos = dao.getLabelByArticleId(Long.valueOf(articleId));
			if(pos != null) {
				data = new HashMap<>();
				data.put("data", pos);
				rs = ResultData.getOk();
				rs.setMsg(pos.size()>0?"获取成功！":"无数据！");
				rs.setData(data);
			}else {
				rs = ResultData.getFail();
				rs.setMsg("获取失败！");
				rs.setData(data);
			}
		}catch(Exception e) {
			e.printStackTrace();
			rs = ResultData.getFail();
			rs.setMsg("未知异常");
		}
		return rs;
	}

	@Override
	public ResultData addLabel(LabelVO vo) {
		ResultData rs = null;
		try {
			if(ObjectUtils.isEmpty(vo)) {
				rs = ResultData.getFail();
				rs.setMsg("参数不得为空！");
				return rs;
			}
			LabelPO po = new LabelPO();
			vo.voToPo(po);
			if(dao.addLabel(po)>0) {
				rs = ResultData.getOk();
				rs.setMsg("添加成功！");
			}else {
				rs = ResultData.getFail();
				rs.setMsg("添加失败！");
			}
		}catch(Exception e) {
			e.printStackTrace();
			rs = ResultData.getFail();
			rs.setMsg("未知异常");
		}
		return rs;
	}

	@Override
	public ResultData delLabel(int id) {
		ResultData rs = null;
		try {
			if(dao.delLabel(Long.valueOf(id))>0) {
				rs = ResultData.getOk();
				rs.setMsg("删除成功！");
			}else {
				rs = ResultData.getFail();
				rs.setMsg("删除失败！");
			}
		}catch(Exception e) {
			e.printStackTrace();
			rs = ResultData.getFail();
			rs.setMsg("未知异常");
		}
		return rs;
	}

	
	@Override
	public ResultData getLabelTypeById(int id) {
		ResultData rs = null;
		Map<String,Object> data = null;
		try {
			LabelTypePO po = dao.getLabelTypeById(Long.valueOf(id));
			if(po != null) {
				data = new HashMap<>();
				data.put("data", po);
				rs = ResultData.getOk();
				rs.setData(data);
				rs.setMsg("获取成功");
			}else {
				rs = ResultData.getFail();
				rs.setMsg("标签不存在！");
			}
		}catch(Exception e) {
			e.printStackTrace();
			rs = ResultData.getFail();
			rs.setMsg("未知异常");
		}
		return rs;
	}

	@Override
	public ResultData getLabelTypeByContent(String content) {
		ResultData rs = null;
		Map<String,Object> data = null;
		try {
			if(StringUtils.isEmpty(content)) {
				rs = ResultData.getFail();
				rs.setMsg("参数不得为空！");
				return rs;
			}
			LabelTypePO po = dao.getLabelTypeByContent(content);
			if(po != null) {
				data = new HashMap<>();
				data.put("data", po);
				rs = ResultData.getOk();
				rs.setData(data);
				rs.setMsg("获取成功");
			}else {
				rs = ResultData.getFail();
				rs.setMsg("获取失败");
			}
		}catch(Exception e) {
			e.printStackTrace();
			rs = ResultData.getFail();
			rs.setMsg("未知异常");
		}
		return rs;
	}

	@Override
	public ResultData addLabelType(LabelTypeVO vo) {
		ResultData rs = null;
		try {
			if(ObjectUtils.isEmpty(vo)) {
				rs = ResultData.getFail();
				rs.setMsg("参数不得为空！");
				return rs;
			}
			LabelTypePO po = new LabelTypePO();
			vo.voToPo(po);
			if(dao.addLabelType(po)>0) {
				rs = ResultData.getOk();
				rs.setMsg("添加成功！");
			}else {
				rs = ResultData.getFail();
				rs.setMsg("添加失败！");
			}
		}catch(Exception e) {
			e.printStackTrace();
			rs = ResultData.getFail();
			rs.setMsg("未知异常");
		}
		return rs;
	}

	@Override
	public ResultData delLabelType(List<Integer> ids) {
		ResultData rs = null;
		try {
			if(ids.size()==0) {
				rs = ResultData.getFail();
				rs.setMsg("参数不得为空！");
				return rs;
			}
			int delSize = dao.delLabelType(ids);
			if(delSize>0) {
				rs = ResultData.getOk();
				rs.setMsg(delSize==ids.size()?"删除成功！":"部分数据未删除成功！");
			}else {
				rs = ResultData.getFail();
				rs.setMsg("删除失败！");
			}
		}catch(Exception e) {
			e.printStackTrace();
			rs = ResultData.getFail();
			rs.setMsg("未知异常");
		}
		return rs;
	}
	
	@Override
	public ResultData getAllLabelType(int pageNum,int pageSize,String keyword) {
		ResultData rs = null;
		Map<String,Object> data = null;
		try {
			PageHelper.startPage(pageNum, pageSize,true);
			List<LabelTypePO> pos = dao.getAllLabelType(keyword);
			if(pos!=null) {
				List<LabelTypeVO> vos = new ArrayList<>();
				pos.forEach(item->{
					LabelTypeVO vo = new LabelTypeVO();
					vo.poToVo(item);
					vos.add(vo);
				});
				PageInfo<LabelTypeVO> pageInfo = new PageInfo<LabelTypeVO>(vos);
				pageInfo.setTotal(dao.getAllLabelType(keyword).size());
				data = new HashMap<>();
				data.put("data", pageInfo);
				rs = ResultData.getOk();
				rs.setMsg(pos.size()>0?"获取成功！":"无数据！");
				rs.setData(data);
			}else {
				rs = ResultData.getFail();
				rs.setMsg("获取失败！");
				rs.setData(data);
			}
		}catch(Exception e) {
			e.printStackTrace();
			rs = ResultData.getFail();
			rs.setMsg("未知异常");
		}
		return rs;	
	}

	@Override
	public ResultData updateLabelType(LabelTypeVO vo) {
		ResultData rs = null;
		try {
			if(ObjectUtils.isEmpty(vo)) {
				rs = ResultData.getFail();
				rs.setMsg("参数不得为空！");
				return rs;
			}
			LabelTypePO po = new LabelTypePO();
			po.setId(Long.valueOf(vo.getId()));
			po.setContent(vo.getContent());
			if(dao.updateLabelType(po)>0) {
				rs = ResultData.getOk();
				rs.setMsg("修改成功！");
			}else {
				rs = ResultData.getFail();
				rs.setMsg("修改失败！");
			}
		}catch(Exception e) {
			e.printStackTrace();
			rs = ResultData.getFail();
			rs.setMsg("未知异常");
		}
		return rs;
	}
}
